﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design

Namespace System.Workflow.ComponentModel.Design
    Friend NotInheritable Class TypeDescriptorContext
        Implements ITypeDescriptorContext, IServiceProvider
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider, ByVal propDesc As PropertyDescriptor, ByVal instance As Object)
            Me.serviceProvider = serviceProvider
            Me.propDesc = propDesc
            Me.instance = instance
        End Sub

        Public Function GetService(ByVal serviceType As Type) As Object
            Return Me.serviceProvider.GetService(serviceType)
        End Function

        Public Sub OnComponentChanged()
            Dim service As IComponentChangeService = DirectCast(Me.serviceProvider.GetService(GetType(IComponentChangeService)), IComponentChangeService)
            If (Not service Is Nothing) Then
                service.OnComponentChanged(Me.instance, Me.propDesc, Nothing, Nothing)
            End If
        End Sub

        Public Function OnComponentChanging() As Boolean
            Dim service As IComponentChangeService = DirectCast(Me.serviceProvider.GetService(GetType(IComponentChangeService)), IComponentChangeService)
            If (Not service Is Nothing) Then
                Try 
                    service.OnComponentChanging(Me.instance, Me.propDesc)
                Catch exception As CheckoutException
                    If (Not exception Is CheckoutException.Canceled) Then
                        Throw exception
                    End If
                    Return False
                End Try
            End If
            Return True
        End Function


        ' Properties
        Public ReadOnly Property Container As IContainer
            Get
                Return DirectCast(Me.serviceProvider.GetService(GetType(IContainer)), IContainer)
            End Get
        End Property

        Public ReadOnly Property Instance As Object
            Get
                Return Me.instance
            End Get
        End Property

        Public ReadOnly Property PropertyDescriptor As PropertyDescriptor
            Get
                Return Me.propDesc
            End Get
        End Property


        ' Fields
        Private instance As Object
        Private propDesc As PropertyDescriptor
        Private serviceProvider As IServiceProvider
    End Class
End Namespace

